<template>
  <div class="wuliu">
    <div class="kflexbc">
        <div class="kflexc">
          <el-upload ref="upload" action="#" :before-upload="uploasd2" :show-file-list="false" accept=".xlsx,.xls">
             <el-button type="danger" round style="margin-right: 20px;">导入添加号码</el-button>
          </el-upload>
          <div @click="xiazai" style="margin-right: 20px; font-size: 12px; color: #999; cursor: pointer;">下载模板</div>
        </div>
      </div>
    <div v-loading='loading' style="padding: 20px;">
      <el-table :data="list" border style="width: 100%; margin: 20px 0;" ref="multipleTable"  @sort-change="handleSortChange" @selection-change="handleSelectionChange">
        <!-- <el-table-column  type="selection"  width="55"></el-table-column> -->
        <el-table-column align='center' label='操作人' >
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column align='center' label='操作人' >
          <template slot-scope="scope">{{scope.row.created_at}}</template>
        </el-table-column>
        <el-table-column align='center' label="操作">
          <template slot-scope="scope">
            <el-button type="primary" v-if="item.status == 2" size="mini" @click="xiazai4(item.id)">下载数据</el-button>
            <el-button type="primary" v-if="item.status != 2 && item.status != -1" :loading="true" size="mini" @click="xiazai4(item.id)">数据生成中,请稍后刷新页面</el-button>
            <div style="margin-bottom: 0;" v-if="item.status == -1"><el-button type="text" size="mini">异常,联系技术解决</el-button></div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align: right;" background layout="total,prev, pager, next" :current-page="all.page" @current-change="fanye" :page-size="all.page_size" :total="total"></el-pagination>
    </div>

  </div>
</template>

<script>
  import top from '../top.vue'
  import diqu from '@/components/productionDelivery/tongyidizhi/zidongshengchanguanli.vue'
  import axios from 'axios'
  import moment from "moment";
  import XLSX from 'xlsx';
  import {
      read,utils
    } from 'xlsx'

  export default {
    name: "wuliu",
    components:{
      diqu,top
    },
    data() {
      return {
        sanlist: [{
        		name: "空闲",
        		id: 1
        	},
        	{
        		name: "预占",
        		id: 20
        	},
        	{
        		name: "已占",
        		id: 30
        	},
        ],
        all2:{
          id:"",
          status:""
        },
        loading:false,
        list:"",
        all:{
          page:1,
          page_size:20,
          pool_id:this.$route.query.id,
          mobile:"",
          status:"",//1空闲 2预占 3已占
          sort:"",
          province:"",
          city:"",
          order_status:""
        },
        total:0,
        shopid:'',
        log:false,
        multipleSelection:"",

        cuowu:[],
        log2:false,
        dangqiandaoru:"",
        relo:true,
        citynum:[]
      }
    },
    created() {
      //店铺权限问题
			this.shopid = JSON.parse(localStorage.getItem('SHOP')).shop_id;
      this.getlist()
      //获取当前年月
      // this.getNowDate()
      // console.log(this.vsdate())


    },
    mounted() {

    },
    methods: {
      handleSortChange:function({ column, prop, order }){
        if(order == null){
          this.all.sort = ''
        }else{
          if(prop == 'updated_at' && order == 'ascending'){
            this.all.sort = 1
          }
          if(prop == 'updated_at' && order == 'descending'){
            this.all.sort = 0
          }
        }
        this.getlist()
      },
      chuandizhi:function(val){
        this.all.province = val.sheng
        this.all.city = val.shi
      },
      xiugaigo:function(){
        axios.post('/api/plan-market/number/status', this.all2)
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message.success('修改成功');
              this.getlist()
              this.log = false
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      shanchu:function(item){
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.post('/api/plan-market/number/delete', {
              id: item.id
            })
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message.success('删除成功');
                this.getlist()
              } else {
                this.$message.error(response.data.msg.info);
              }
            })
        })
      },
      daochu:function(){
        axios.get('/api/plan-market/pool/numbers/export',{params:this.all})
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message.success('导出成功');
              this.getlist()
              this.goopen('gongju/order_export_haoma')
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      shifangduo:function(){
        if(this.multipleSelection != ''){
          let arr = []
          this.multipleSelection.forEach(item=>{
            arr.push(item.id)
          })
          this.$confirm('确定释放吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            axios.post('/api/plan-market/numbers/release', {
                ids: arr
              })
              .then(response => {
                if (response.data.msg.code == 0) {
                  this.$message.success('释放成功');
                  this.getlist()
                } else {
                  this.$message.error(response.data.msg.info);
                }
              })
          })
        }else{
          this.$message.error('先选择要释放的号码');
        }
      },
      shanchuduo:function(){
        if(this.multipleSelection != ''){
          let arr = []
          this.multipleSelection.forEach(item=>{
            arr.push(item.id)
          })
          this.$confirm('确定删除吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            axios.post('/api/plan-market/new/numbers/delete', {
                ids: arr
              })
              .then(response => {
                if (response.data.msg.code == 0) {
                  this.$message.success('删除成功');
                  this.getlist()
                } else {
                  this.$message.error(response.data.msg.info);
                }
              })
          })
        }else{
          this.$message.error('先选择要删除的号码');
        }

      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      logkai:function(item){
        this.all2 = {
          id:item.id,
          status:item.status
        }

        this.log = true
      },
      readExcel1(files) {
        return new Promise((res, rej) => {
          var that = this;
          if (files.length <= 0) {
            return;
          }
          if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            return;
          }
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = read(data, {
                type: 'binary'
              });
              // 取第一张表
              const wsname = workbook.SheetNames[0];
              // 生成json表格内容
              const ws = utils.sheet_to_json(workbook.Sheets[wsname]);
              console.log(ws);
              // 后续为自己对ws数据的处理
              res(ws)
            } catch (e) {
              return false;
              rej(e)
            }
          };
          fileReader.readAsBinaryString(files[0]);
        })

      },

      uploasd2(file, fileList) {
        var that = this

        console.log(file)
        // let files = {
        //   0: file
        // }

        var formData = new FormData();
        formData.append('pool_id', this.$route.query.id);
        formData.append('file', file);


        axios.post('/api/plan-market/numbers/import',formData)
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message.success(`导入成功`);
                this.getlist()
              } else {
                if(response.data.msg.code == 51043){



                  response.data.msg.info = JSON.parse(response.data.msg.info)
                  this.cuowu = response.data.msg.info

                  this.log2 = true
                }else{
                  this.$message.error(response.data.msg.info);
                }

              }
            })

        // let ff = this.readExcel1(files);
        // ff.then((res) => {

        //   res.forEach(item => {
        //     let obj = {
        //       mobile: item['手机号码'],
        //       province: item['省'],
        //       city: item['市'],
        //     }
        //     data.numbers.push(obj)
        //   })

        //   this.dangqiandaoru = data


        // })
      },
      qiangzhi:function(){
         this.dangqiandaoru.force_import = 1
        axios.post('/api/plan-market/numbers/import', this.dangqiandaoru)
          .then(response => {
            if (response.data.msg.code == 0) {
              this.log2 = false
              this.$message.success(`导入成功`);
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },

      uploasd3(file, fileList) {
        var that = this
        let files = {
          0: file
        }
        let data = {
          pool_id:this.$route.query.id,
          numbers:[]
        }

        let ff = this.readExcel1(files);
        ff.then((res) => {

          res.forEach(item => {
            data.numbers.push(item['手机号码'])
          })

          axios.post('/api/plan-market/numbers/import/release',data)
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message.success(`导入成功`);
                this.getlist()
              } else {
                this.$message.error(response.data.msg.info);
              }
            })
        })
      },

      //统计

      search: function() {
        this.all.page = 1
        this.getlist()
      },
      //翻页
      fanye: function(page) {
        this.loading = true
        this.all.page = page
        this.getlist()
      },
      //获取列表
      getlist: function() {
        axios.get('/api/plan-market/export/log2?order_by=0&type=35')
          .then(response => {
            if (response.data.msg.code == 0) {
              this.list = response.data.data.data
              // this.total = response.data.data.total
              this.loading = false
              console.log('response.data.data.data====')
              console.log(response.data.data)
            } else {
              this.$message.error(response.data.msg.info);
            }
          })


          axios.post('/api/plan-market/number/statistics',{
            pool_id:this.$route.query.id
          })
          .then(response => {
            if (response.data.msg.code == 0) {
              console.log('city')
              console.log(response.data.data)
              this.citynum = response.data.data
            } else {
              this.$message.error(response.data.msg.info);
            }
          })

      },

      //下载模板
      xiazai: function() {
        location.href = 'https://file.storep.91haoka.cn/gantanhao/haochihaoma.xlsx?attname='
      },

      //真下载
      xiazai3: function(dizhi) {
        location.href = 'https://file.storep.91haoka.cn/gantanhao/shifanghaoma.xlsx?attname='
      },


    }
  }
</script>

<style lang="scss" scoped>
  .wuliu {
    .fen{
      .el-button--danger{
            color: #2974FF;
            background-color: #EEF7FF;
            border-color: #EEF7FF;
      }

    }
    .powertop {
    	display: flex;
    	background:#f9f9f9;
    }
    .powertopli {
    	flex-basis: 180px;
    	line-height: 50px;
    	border-bottom: 3px #F9F9F9 solid;
    	color: #727272;
    	border-right: 1px #F9F9F9 solid;
    	font-size: 16px;
    	text-align: center;
    }
    .router-link-active {
    	background: #FFFFFF;
    	border-bottom: 3px #2974FF solid;
    }
    /deep/ .el-pagination.is-background .btn-prev:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .btn-prev{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active{ background-color: #2974FF;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active:hover {
        color: #fff;
    }
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled):hover {
        color: #2974FF;
    }
    /deep/ .el-pager li{ border-radius: 14px; background: rgba(255, 255, 255, 1); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .btn-prev{ border-radius: 14px; padding: 0 10px;}
    /deep/ .btn-next{ border-radius: 14px; padding: 0 10px;}

    /deep/ .el-table th{ background: rgb(246, 246, 246);}
    /deep/ .el-input__inner{ line-height: 36px; height: 36px; font-size: 12px; background: rgb(247, 248, 250);  border: 0;}
    /deep/ .el-date-editor .el-range-input{ background: rgb(247, 248, 250); }
    /deep/ .el-button{ padding: 11px 23px !important;}
    /deep/ .el-button--mini{ padding: 6px 15px !important;}
    /deep/ .el-button--danger {
        color: #FFF;
        background-color: #2974FF;
        border-color: #2974FF;
    }
    .team {
    	background: #F9F9F9;
    }
  }
</style>
